<?php
/**
* * Model Class
* 
* @author Guillermo Landín
* @link 
* 
* 
*/ 

class Publicitymodel extends Model {

    function __construct()
    {
        parent::Model();
        
        $this->CI =& get_instance(); 
    }

    //returns all the details for single item $id for display in publicity detail page
    public function get_item($id = NULL){

        $this->db->select('Title_Publicity, Date_Publicity, id_Publicity, Introduction_Publicity, Description_Publicity, Image_Publicity')
                ->from('publicity')
                ->where('id_Publicity', $id)
                ->where('Active_Publicity', 1)
                ->order_by('Date_Publicity', 'DESC');       

        $query = $this->db->get();

        return $query->row_array();

    }

    //returns the details from the latest publicity item, for display as the default item in publicity home page
    public function get_latest_publicity_item(){
    
         $this->db->select('Title_Publicity, Date_Publicity, id_Publicity, Introduction_Publicity, Description_Publicity, Image_Publicity')    
                ->from('publicity')
                ->where('Active_Publicity', 1)
                ->order_by('Date_Publicity', 'DESC')
                ->limit(1);       

        $query = $this->db->get();  

        return $query->row_array();    

    }
    
    //returns the list of publicity, grouped in an array of months. list can be limited to items of a certain $month
    function get_list($month = NULL){
    
        $this->db->select('Title_Publicity AS title, Date_Publicity AS date, id_Publicity AS id, Introduction_Publicity AS text')
                ->from('publicity')
                ->where('Active_Publicity', 1)
                ->order_by('Date_Publicity', 'DESC');  
        
        if ($month) {
            $date_mysql = date('Y-m-d', strtotime($month));
            $this->db->where("Date_Publicity >= '$date_mysql' AND Date_Publicity <= LAST_DAY('$date_mysql')");
         }

        $query = $this->db->get();

        $result = $query->result_array();

        $grouped_result = $this->group_into_months($result);
                                 
        return $grouped_result;

    }

    //receives a db result and returns a new array, with results grouped in arrays of months    
    private function group_into_months($result = array())
    {
        
        $grouped_array = array();

        foreach($result as $row) 
        {
             $month_display = date('F Y', strtotime($row['date']));
             $month_param = strtolower(date('MY', strtotime($row['date'])));


             if (@$grouped_array[count($grouped_array)-1]["name"] == $month_display)
             {
                $grouped_array[count($grouped_array)-1]["items"][] = $row;

             } else {
                
                $grouped_array[] = array(
                                        "name" => $month_display,
                                        "params" => $month_param,     
                                        "items" => array($row)
                                        );
                
             }

        }

        //print "<pre>";
        //print_r($grouped_array);
        //print "</pre>";

        return $grouped_array;

    }


/***********************************   
*   Sub-menu functions
***********************************/

    //returns an array of months with fields 'name', 'controller' and 'param'
    public function get_submenu_months(){

        $this->db->select('DISTINCT DATE_FORMAT(Date_Publicity,\'%M %Y\') AS name', FALSE)
                ->from('publicity')
                ->where('Active_Publicity', 1)
                ->order_by('Date_Publicity', 'DESC');  

        $query = $this->db->get();

        $result = $query->result_array();     

        foreach($result as &$row){
            $row = array( 
                        'name' => $row['name'], 
                        'controller' => 'publicity/month/'.strtolower(date('MY', strtotime($row['name']))), 
                        'param' => strtolower(date('MY', strtotime($row['name'])))
                        );
        }

        return $result;

    }

    //returns an array of months with content for given month
    public function get_submenu_by_month($month = FALSE){

        //get the full array of months
        $full_result = $this->get_submenu_months();  

        //get the items for the selected month      
        $month_items = $this->get_submenu_items_by_month($month); 

         //insert array of items into array of months
        foreach($full_result as &$month_row){
            if ( strtotime($month_row['name']) == strtotime($month) ) {

                $month_row['selected'] = TRUE;
                $month_row['content'] =  $month_items;
            
            }
        }

        return $full_result;

    }

    //returns an array of months with content for given id
    public function get_submenu_by_id($id = FALSE){

        //get the full array of months
        $full_result = $this->get_submenu_months();  

        //get the month for the given item id
        $month = $this->get_item_month($id);  

        //get the items for the selected month
        $month_items = $this->get_submenu_items_by_month($month); 

        //set the selected item
        foreach($month_items as &$item){

            if ( $item['param'] == $id) $item['selected'] = TRUE;

        }

         //insert array of items into array of months
        foreach($full_result as &$month_row){

            if ( $month_row['param'] == $month ) {

                $month_row['selected'] = TRUE;
                $month_row['content'] =  $month_items;
            
            }

        }

        return $full_result;

    }

    //returns the month for a given item id (ex:jan2009)
    private function get_item_month($id) {

        $this->db->select('Date_Publicity')
                ->from('publicity')
                ->where('id_Publicity', $id)
                ->where('Active_Publicity', 1);

        $query = $this->db->get();

        $result = $query->row_array();

        $month = strtolower(date('MY', strtotime($result['Date_Publicity'])));

        return $month;

    }
    
    //returns an array of items for a given month with fields 'name', 'controller' and 'param'
    private function get_submenu_items_by_month($month = FALSE){

        $this->db->select('id_Publicity, Title_Publicity')
                ->from('publicity')
                ->where('Active_Publicity', 1) 
                ->order_by('Date_Publicity', 'DESC'); 
        if ($month) {
            $date_mysql = date('Y-m-d', strtotime($month));
            $this->db->where("Date_Publicity >= '$date_mysql' AND Date_Publicity <= LAST_DAY('$date_mysql')");
         }

        $query = $this->db->get();
        $result = $query->result_array(); 

        foreach($result as &$row){
            $row = array( 
                            'name' => $row['Title_Publicity'], 
                            'controller' => 'publicity/view/'.$row['id_Publicity'], 
                            'param' => $row['id_Publicity']
                            );
        }

        return $result;

    }


/***********************************
*   Backoffice functions
***********************************/

    //returns the list of publicity
    function admin_get_list($fields='*'){
    
        $this->db->select($fields)
                ->from('publicity')
                ->order_by('Date_Publicity', 'DESC');  

        $query = $this->db->get();

        $result = $query->result_array();
                                 
        return $result;

    }  
    public function admin_get_item($id=FALSE, $fields='*'){

        $this->db->select($fields)
                ->from('publicity')       
                ->where('id_Publicity', $id);  
        
        $query = $this->db->get();
        return $query->row_array();

    }  
    public function admin_update($id=FALSE, $data=array()){

        $this->db->where('id_Publicity', $id)
                ->update('publicity', $data); 

    }                      
    public function admin_insert($data=array()){

        $data["id_Publicity"] = uniqid();
        $data["Active_Publicity"] = 0;
        $this->db->insert('publicity', $data); 
        return $data["id_Publicity"];

    }  
    public function admin_delete($id=FALSE){

        $this->db->where('id_Publicity', $id);
        $this->db->delete('publicity'); 

    } 


}
